import axios from "axios";
import router from "../router";
const request = axios.create({
    baseURL: 'http://127.0.0.1:8080'
})
const actionMapping = {
    get: 'view',
    post: 'add',
    put: 'edit',
    delete:'delete'
}
request.interceptors.request.use(config => {
    if (config.url !== '/login' && config.url!=='/admin'){
        config.headers.Authorization = localStorage.getItem('userToken')
        // 获取请求方式
        const method = config.method
        // 根据请求方式得到是那种操作
        const action = actionMapping[method] //add
        const rights = router.currentRoute.meta   //view  
        // 判断action是否存在当前路由权限中
        if (rights && rights.indexOf(action)==-1) {
            alert('没有权限')
            return Promise.reject(new Error('没有权限'))
        }  
    }
    return config
}, err => {
    return Promise.reject(err)
})
request.interceptors.response.use(response => {
    return response.data
}, err => {
    return Promise.reject(err)
})
export default request